package com.example.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.entity.Argue;
import com.example.blog.entity.ArgueReply;
import com.example.blog.entity.User;
import com.example.blog.mapper.ArgueMapper;
import com.example.blog.service.impl.ArgueReplyServiceImpl;
import com.yc.common.biz.BizException;
import com.yc.common.vo.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘扬新
 * @since 2023-06-30
 */
@RestController
@RequestMapping("/blog/argue")
public class ArgueController {

    @Resource
    ArgueMapper argueMapper;


    //查询争锋相对
    @RequestMapping("queryPage")
    public List<Argue> queryPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size){
        Page<Argue> P = new Page<>(page,size);
        return argueMapper.selectPage(P,null).getRecords();
    }

    //查询争锋相对详细
    @RequestMapping("queryById")
    public Argue queryById(int id){
        return argueMapper.selectById(id);
    }

    @Resource
    ArgueReplyServiceImpl argueReplyService;

    @RequestMapping("submit")
    public Result submit(@RequestBody ArgueReply reply, @SessionAttribute User myinfo){
        try{
            reply.setUid(myinfo.getId());
            argueReplyService.sumbit(reply);
            return Result.success("发表成功！");
        }catch (BizException e) {
            return Result.failure(e.getMessage());
        }
    }
}
